<?php

namespace Chap10Bundle\Controller;

use Chap10Bundle\Entity\Product;
use Chap10Bundle\Entity\Category;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Translation\Dumper\PoFileDumper;

class DefaultController extends Controller
{
    /**
     * @Route("/", name="chap10_default_homepage")
     */
    public function indexAction()
    {
        return $this->render('Chap10Bundle:Default:index.html.twig');
    }


    /**
     * @Route("/new")
     * @return Response
     */
    public function createAction()
    {
        $product = new Product();
        $product->setName('iphone 6s');
        $product->setPrice(699);
        $product->setDescription("New generate iphone 6s.");

        $em = $this->getDoctrine()->getManager();
        $em->persist($product);
        $em->flush();

        return new Response('saved new product with id=' . $product->getId());
    }


    /**
     * @Route("/shows/{pid}")
     * @param $pid
     * @return Response
     */
    public function showAction($pid)
    {
        $product = $this->getDoctrine()
            ->getRepository('Chap10Bundle:Product')
            ->find($pid);

        if (!$product) {
            throw $this->createNotFoundException('No product for id=' . $pid);
        }

        var_dump($product);

        return new Response();
    }


    /**
     * @Route("/shows2/{pid}")
     * @param $pid
     * @return Response
     */
    public function show2Action($pid=1)
    {
        $repository = $this->getDoctrine()
            ->getRepository('Chap10Bundle:Product');

        $product1 = $repository->find($pid);
        var_dump($product1);

        echo "<hr>";

        $product2 = $repository->findOneById($pid);
        var_dump($product2);

        echo "<hr>";

        $product3 = $repository->findOneByPrice(599);
        var_dump($product3);

        echo "<hr>";

        $products = $repository->findAll();
        var_dump($products);

        return new Response();

    }


    /**
     * @Route("/update/{pid}")
     * @param $pid
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
     */
    public function updateAction($pid)
    {
        $em = $this->getDoctrine()
            ->getManager();

        $product = $em->getRepository('Chap10Bundle:Product')->find($pid);

        if (!$product) {
            throw $this->createNotFoundException('No product found for id'.$pid);
        }

        $product->setName('New product name');
        $em->flush();


        return $this->redirectToRoute("chap10_default_homepage");
    }


    /**
     * @Route("/delete/{pid}")
     * @param $pid
     * @return Response
     */
    public function deleteActoin($pid)
    {
        $em = $this->getDoctrine()
            ->getManager();

        $product = $em->getRepository('Chap10Bundle:Product')->find($pid);

        if (!$product) {
            throw $this->createNotFoundException('No product found for id'.$pid);
        }

        $em->remove($product);
        $em->flush();

        var_dump("done!");

        return new Response();
    }


    /**
     * @Route("/query")
     * @return Response
     */
    public function queryAction()
    {
        $em = $this->getDoctrine()
            ->getManager();

        $sql = "SELECT p FROM Chap10Bundle:Product p WHERE p.price > :price ORDER BY p.price ASC";
        $query = $em->createQuery($sql)->setParameter('price', 500);

        $products = $query->getResult();

        $product = $query->setMaxResults(1)->getOneOrNullResult();

        var_dump($products);
        var_dump($product);

        return new Response();
    }


    /**
     * @Route("/query2")
     * @return Response
     */
    public function query2Action()
    {
        $repository = $this->getDoctrine()
            ->getRepository('Chap10Bundle:Product');

        // createQueryBuilder automatically selects fROM Chap10Bundle:Product
        // and aliases it to 'p'
        $query = $repository->createQueryBuilder('p')
            ->where('p.price > :price')
            ->setParameter('price', "500")
            ->orderBy('p.price', 'ASC')
            ->getQuery();

        $products = $query->getResult();

        var_dump($products);

        return new Response();
    }


    /**
     * @Route("/query3")
     * @return Response
     */
    public function query3Container()
    {
        $em = $this->getDoctrine()
            ->getRepository('Chap10Bundle:Product');
        $products = $em->findAllOrderByName();

        var_dump($products);

        return new Response();
    }


    /**
     * Saving Related Entities
     *
     * @Route("/newprd")
     * @return Response
     */
    public function createProductAction()
    {
        $category = new Category();
        $category->setName("Computer and Office");

        $product = new Product();
        $product->setName("Keyboard");
        $product->setPrice(19.19);
        $product->setDescription("Ergonomic and stylish!");
        $product->setCategory($category);

        $em = $this->getDoctrine()->getManager();
        $em->persist($category);
        $em->persist($product);
        $em->flush();

        $cateid = $category->getId();
        $pid = $product->getId();
        return new Response(
            'saved new product with id='.$pid.
            ' and new category with cid='.$cateid
        );

    }


    /**
     * @Route("/showprd/{pid}")
     * @param $pid
     * @return Response
     */
    public function showProductAction($pid)
    {
        $product = $this->getDoctrine()
            ->getRepository('Chap10Bundle:Product')
            ->find($pid);

        $categoryName = $product->getCategory()->getName();

        $category = $product->getCategory();
        dump($category);

        return new Response();

    }


    /**
     * @Route("/showprd2/{pid}")
     * @param $pid
     */
    public function showProduct2Action($pid)
    {
        $product = $this->getDoctrine()
            ->getRepository('Chap10Bundle:Product')
            ->findOneByIdJoinedToCategory($pid);

        $category = $product->getCategory();

        var_dump($product);
        var_dump($category);
        var_dump(1111);

        return new Response();
    }
}
